//
//  SAnimation.m
//  LSCodeLibrary
//
//  Created by Samuel on 15/3/31.
//  Copyright (c) 2015年 Samuel. All rights reserved.
//

#import "SAnimation.h"
#define degreesToRadians(x)(M_PI*x/180.0)
@interface SAnimation ()

@end
@implementation SAnimation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


+ (void)zoomIn: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait{
    view.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:duration animations:^{
        view.transform = CGAffineTransformIdentity;
        //view.transform = CGAffineTransformMakeScale(0, 0);
        //view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}
+ (void)buttonPressAnimate: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait{
    //Usually Changes the position
    [UIView animateWithDuration:duration animations:^{
        view.transform = CGAffineTransformMakeScale(1.05, 1.05);
        view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}

+ (void)fadeIn: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait{
    [UIView animateWithDuration:duration animations:^{
        [view setAlpha:1.0];
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}
+ (void)fadeOut: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait{
    [UIView animateWithDuration:duration animations:^{
        [view setAlpha:0.0];
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}
+ (void) moveLeft: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
    [UIView animateWithDuration:duration animations:^{
        view.center = CGPointMake(view.center.x - length, view.center.y);
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}

+ (void) moveRight: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
    [UIView animateWithDuration:duration animations:^{
        view.center = CGPointMake(view.center.x + length, view.center.y);
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}

+ (void) moveUp: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
    [UIView animateWithDuration:duration animations:^{
        view.center = CGPointMake(view.center.x, view.center.y-length);
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}

+ (void) moveDown: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
    [UIView animateWithDuration:duration animations:^{
        view.center = CGPointMake(view.center.x, view.center.y + length);
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}

+ (void) rotate: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andAngle:(int) angle{
    [UIView animateWithDuration:duration animations:^{
        view.transform = CGAffineTransformMakeRotation(degreesToRadians(angle));
    } completion:^(BOOL finished) { }];
    // wait for animation to finish
    if (wait) {
        [SAnimation waitRunloopWith:duration];
    }
}

+ (void)shakeView:(UIView*)view isOpen:(BOOL)bools
{
    CALayer*viewLayer=[view layer];
    if (bools) {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
        animation.duration=0.1;
        animation.repeatCount = 100000;
        animation.autoreverses=YES;
        animation.fromValue=[NSValue valueWithCATransform3D:CATransform3DRotate
                             (viewLayer.transform, -0.03, 0.0, 0.0, 0.03)];
        animation.toValue=[NSValue valueWithCATransform3D:CATransform3DRotate
                           (viewLayer.transform, 0.03, 0.0, 0.0, 0.03)];
        [viewLayer addAnimation:animation forKey:@"wiggle"];
    }
    else{
        [viewLayer removeAllAnimations];
    }
}

+ (void)waitRunloopWith:(float)time; {
    float i = time;
    while (i>0){
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
        i = i-0.01;
    }
}

@end

